<template>
  <div id="invitationPolite">
    <h1 class="con-right-title">发奖管理</h1>
    <topSearch @toSearch="toSearch"></topSearch>
    <div class="table-conBtn">
      <el-button type="warning" @click="exportOrderList"
        >导出全部订单</el-button
      >
      <el-button
        type="warning"
        icon="el-icon-download"
        @click="importJDcard('virtual')"
        >批量发放京东E卡</el-button
      >
      <el-button type="warning" @click="importJDcard('entity')"
        >批量发放实体订单</el-button
      >
      <freshCom @searchList="searchList"></freshCom>
    </div>
    <el-table :data="tableData" ref="table">
      <el-table-column type="index" label="序号" width="80" align="center">
      </el-table-column>
      <el-table-column
        prop="activityInfo.activityName"
        label="活动名称"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="activityInfo.activityType"
        label="活动类型"
        min-width="100"
        align="center"
      >
        <template v-slot="scope">
          <span>{{
            scope.row.activityInfo.activityType
              | filterName('activity_order_types')
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="awardInfo.awardName"
        label="商品名称"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="orderTime"
        label="订单时间"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="giveAwardState"
        label="发放状态"
        min-width="100"
        align="center"
      >
        <template v-slot="scope">
           <div v-if="scope.row.awardInfo.awardType === 'VIRTUAL'">
            <span v-if="scope.row.giveAwardState === 'YES'">已发奖</span>
            <span v-if="scope.row.giveAwardState === 'NO'">未发奖</span>
           </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="giveAwardState"
        label="快递状态"
        min-width="80"
        align="center"
      >
      <template v-slot="scope">
        <div v-if="scope.row.awardInfo.awardType === 'ENTITY'">
          <span v-if="scope.row.giveAwardState === 'YES'">已发货</span>
          <span v-if="scope.row.giveAwardState === 'NO'">未发货</span>
        </div>
      </template>
      </el-table-column>
      <el-table-column
        prop="user.nickName"
        label="用户名"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="user.dpAccount"
        label="大鹏号"
        min-width="80"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="deliveryAddress.name"
        label="收货人"
        min-width="80"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="deliveryAddress.phone"
        label="收货人手机号"
        min-width="80"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="deliveryAddress.particularAddress"
        label="地址"
        min-width="80"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="goodsType"
        label="商品类型"
        min-width="80"
        align="center"
      >
        <template v-slot="scope">
          <span>{{
            scope.row.awardInfo.awardType
              | filterName('activity_order_order_types')
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="awardInfo.awardAmount"
        label="数量"
        min-width="80"
        align="center"
      >
      </el-table-column>
      <el-table-column label="操作" min-width="120" align="center">
        <template v-slot="scope">
          <el-button type="text" v-if="isEntityShow(scope.row)" @click="deliverGoods(scope.row)">发货</el-button>
          <el-button type="text" v-if="isVirtualShow(scope.row)" @click="deliverGoods(scope.row)">发放</el-button>
          <el-button type="text" v-if="isShowSee(scope.row)" @click="toView(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pageCom
      :searchParams="searchParams"
      :total="total"
      @searchList="searchList"
    ></pageCom>
    <!-- 导入弹窗 -->
    <importDialog
      v-if="importDialogData.isShow"
      :dialog-data="importDialogData"
      @refresh="searchList"
      @showError="showError"
      @uploadSuccess="uploadSuccess"
    ></importDialog>
     <!-- 导入失败弹窗 -->
    <errorDialog
      v-if="errorDialogData.isShow"
      :dialog-data="errorDialogData"
    ></errorDialog>
    <!--  -->
    <!--  -->
    <!-- 导入成功的弹窗 -->
    <importSuccessDialog
      v-if="importSuccessDialogData.isShow"
      :dialog-data="importSuccessDialogData"
      :orderType="this.orderType"
    ></importSuccessDialog>
    <!-- 发货-实体订单发货弹窗 -->
    <deliverGoodsDialog
      ref="deliverGoodsDialog"
      @confirm="deliverGoodsConfirm"
    ></deliverGoodsDialog>
    <!-- 发放-发放京东卡弹窗 -->
    <setup-password
      ref="setupPasswordDialog"
    ></setup-password>
    <!-- 查看卡密信息 -->
    <linkCardMessage
      :linkCardMessage="linkCardMessage"
      v-if="linkCardMessage.isShow"
    ></linkCardMessage>
    <!-- 查看实体订单信息 -->
    <physicalsPrize
      :physicalsPrize="physicalsPrize"
      v-if="physicalsPrize.isShow"
    ></physicalsPrize>
  </div>
</template>
<script>
import { sysNormalDisable } from '@utils/dictionariesFunc'
import topSearch from './topSearch'
import {
  getOrderList,
  importJDcards,
  importJDcardsTemplate,
  downloadFailData,
  deliverGood,
  getDetail
} from '@/api/activityCenter/prizeManage'
import importDialog from '@common/importDialog' // 导入弹窗
import errorDialog from '@common/errorDialog' // 导入失败弹窗
import importSuccessDialog from './importSuccessDialog' //导入成功弹窗
import deliverGoodsDialog from './deliverGoodsDialog'
import setupPassword from './setupPasswordDialog'
import linkCardMessage from './linkCardMessageDialog'
import physicalsPrize from './physicalsPrizeDialog'
export default {
  components: {
    topSearch,
    importDialog,
    errorDialog,
    importSuccessDialog,
    deliverGoodsDialog,
    setupPassword,
    linkCardMessage,
    physicalsPrize
  },
  data() {
    return {
      loading: false,
      tableData: [],
      total: 0,
      forminline: {
        phone: '',
        dpAccount: '',
        // 订单号
        courierNumber: '',
        orderTimeStart: '',
        orderTimeEnd: '',
        awardName: '',
        // 商品类型 奖品类型:[ENTITY,VIRTUAL]
        awardType: '',
        // 商品发放状态,可用值:YES,NO
        giveAwardState: '',
        // 活动类型,[PRAISE,RECOMMEND]
        activityType: '',
        activityName: ''
      },
      searchParams: {
        pageNum: 1,
        pageSize: 10
      },
      /* 虚拟信息设置 */

      setupPassword: {
        isShow: false,
      },
      /* 查看卡密信息 */
      // linkCardMessage
      linkCardMessage: {
        isShow: false,
        formData: []
      },
      /* 查看实体信息 */
      physicalsPrize: {
        isShow: false,
        formData:{}
      },
      // 传递给导入弹框的数据
      importDialogData: {
        type: 'partnerOrder',
        title: '发奖管理列表导入',
        isShow: false, // 控制弹框显示
        params: '' // 参数
      },
      // 导入失败弹框的数据
      errorDialogData: {
        isShow: false, // 控制弹框显示
        warningText: '' // 参数
      },
      setupPasswordDialogData: {
        isShow: false
      },
      importSuccessDialogData: {
        isShow: false,
        successText: { successNum: 0, failNum: 0 },
        orderType:''
      },
      orderType:''
    }
  },
  async mounted() {
    this.searchList()
    // 活动类型
    await sysNormalDisable('activity_order_types', 'activity_order_types')
    // 商品类型
    await sysNormalDisable(
      'activity_order_order_types',
      'activity_order_order_types'
    )
  },
  methods: {
    /* 列表 */
    async searchList() {
      this.loading = true
      let obj = Object.assign({}, this.searchParams, this.formInline)
      let params = obj
      getOrderList(
        params,
        (res) => {
          this.loading = false
          this.tableData = res.rows
          this.total = res.total
        },
        () => {
          this.loading = false
        }
      )
    },
    /* 搜索 */
    toSearch(val) {
      this.searchParams.pageNum = 1
      Object.assign(this.searchParams, val)
      this.searchList()
    },
     /* 显示发货按钮 */
    isEntityShow (row) {
      return row.awardInfo.awardType === 'ENTITY' && row.giveAwardState === 'NO'
    },
     /* 显示发放按钮 */
    isVirtualShow (row) {
      return row.awardInfo.awardType === 'VIRTUAL' && row.giveAwardState === 'NO'
    },
    // 显示查看按钮
    isShowSee(row) {
      return row.giveAwardState === 'YES'
    },
    /* 导出全部订单 */
    exportOrderList() {
      this.$confirm('确定导出全部订单吗？', '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          let params = Object.assign({}, this.formInline)
          let url = '/activities/orders/export'
          this.downFile('get', url, params, '发奖管理全部订单')
        })
        .catch(() => {})
    },
    // 批量发放京东E卡 导入京东E卡
    importJDcard(type) {
      this.orderType = type
      type ===  'virtual' ? this.importDialogData.title = '批量发放京东E卡' : this.importDialogData.title = '批量发放实体订单'
      // 下载参数定义
      const exportType =  type ===  'virtual' ? 'JINGDONGECARD' : 'PHYSICALOBJECTS'
      this.importDialogData.url = importJDcards(type) // 导入接口
      this.importDialogData.downUrl = importJDcardsTemplate(exportType) // 下载模板接口
      this.importDialogData.params = 'JD'
      this.importDialogData.isShow = true // 显示导入弹框
    },
    // 批量发放京东E卡 导入京东E卡 成功弹窗
    uploadSuccess(val) {
      this.importSuccessDialogData = {
        isShow: true,
        successText: val,
        failDataUrl: downloadFailData(this.orderType),
        orderType: this.orderType
      }
    },
     /* 批量发放京东E卡 导入京东E卡 导入失败弹窗 */
    showError(text) {
      this.errorDialogData.isShow = true // 显示导入失败弹框
      this.errorDialogData.warningText = text // 显示导入失败弹框
    },
    /* 发放和发货按钮绑定功能，根据是实体或虚拟物品判断调用接口 */
    deliverGoods(row) {
      const awardType = row.awardInfo.awardType
      // VIRTUAL 虚拟 ENTITY实体
      if (awardType === 'VIRTUAL') {
         this.$refs.setupPasswordDialog.show(row)
      } else {
        this.$refs.deliverGoodsDialog.show(row)
      }
    },
    /* 发货弹窗-确定功能 */
    deliverGoodsConfirm(row, form) {
      const type = row.awardInfo.awardType.toLowerCase()
      const params = {
        logisticsCompany: form.logisticsCompany,
        courierNumber: form.courierNumber
      }
      this.$confirm('是否该实物物品进行了发货？', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        this.deliverGoodsAction(row.id, params ,type,'发货成功！')
      })
    },
    /* 发货或发放功能调用接口 */
    deliverGoodsAction(id, params, type,confirmSuccessInfo) {
      deliverGood(
        id,
        params,type,
        (response) => {
          this.searchList()
          this.$message({
            type: 'success',
            message: confirmSuccessInfo
          })
        },
        (response) => {
          this.$message({
            message: response.message,
            type: 'warning'
          })
        }
      )
    },
     /* 查看按钮 */
    toView(row) {
      const awardType = row.awardInfo.awardType
      getDetail(row.id,
        (response) => {
          if (awardType === 'VIRTUAL'){
            this.linkCardMessage.formData = response.cards
            this.linkCardMessage.isShow = true
          } else {
            this.physicalsPrize.formData = response.deliveryAddress
            this.physicalsPrize.isShow = true
          }
        },
        (response) => {
          this.$message({
            message: response.message,
            type: 'warning'
          })
        }
      )
    }
  }
}
</script>
<style lang="less" scoped>
</style>
